AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include('shared.lua')

function ENT:Initialize()
	self:SetModel(self.Model)
	self:SetColor(self.Color.r, self.Color.b, self.Color.g, self.Color.a)
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetMoveType(MOVETYPE_VPHYSICS)
	self:SetSolid(SOLID_VPHYSICS)
	self:PhysWake()
	
	self.dt.Health = self.HP
	if self.SparkPos then
		self.dt.Sparking = self.Sparking
	end
	if self.Power > 0 then
		self.dt.Power = 0
	end
	if self.Upgrades then
		self.dt.Upgrade = self.Upgrade
	end
	
	if self.CreateTimers then self:CreateTimers() end
end

function ENT:SpawnFunction(ply, tr)
	local SpawnPos = tr.HitPos + tr.HitNormal * 42

	local ent = ents.Create(self.ClassName)
	ent:SetPos(SpawnPos)
	ent.dt.Owner = ply
	ent:Spawn()
	ent:Activate()
	ent.OwnerID = ply:UniqueID()
		
	return ent
end

function ENT:Think()
	if (!ValidEntity(self.dt.Owner)) then
		if(self.Save) then 
			if(!timer.IsTimer(tostring(self.Entity) .. "fail") && !timer.IsTimer(tostring(self.Entity) .. "findowner")) then
				self.World = true
				self.Entity:SetColor(0,255,0,255)
				timer.Create( tostring(self.Entity) .. "findowner", 1, 120, self.OwnerTick, self)
				timer.Create( tostring(self.Entity) .. "fail", 122, 1, self.OwnerFail, self)
			end
		else
			self.Entity:Remove()
		end
	end
end

function ENT:OwnerTick()
	local ply = player.GetByUniqueID(self.OwnerID)
	if (ValidEntity(ply)) then
		if (!timer.IsTimer(tostring(self.Entity) .. "foundowner")) then
			//this gives their game time to properly load
			timer.Create( tostring(self.Entity) .. "foundowner", 2, 1, self.FoundOwner, self)
		end
	end
end

function ENT:FoundOwner()
	local ply = player.GetByUniqueID(self.OwnerID)
	self.dt.Owner = ply
	self.World	  = false
	self.Entity:CrashReset()
	Notify(ply,0,3,"A ".. self.PrintName .." is active where you left it")
	timer.Destroy(tostring(self.Entity) .. "findowner")
	timer.Destroy(tostring(self.Entity) .. "fail")
end

function ENT:OwnerFail()
	timer.Destroy(tostring(self.Entity) .. "findowner")
	timer.Destroy(tostring(self.Entity) .. "fail")
	self.Entity:Remove()
end

function ENT:CrashReset()
	self.Entity:SetColor(255,255,255,255)
end

function ENT:PayDay()
	local owner = self.dt.Owner
	if self.Active && !self.World then
			owner:GiveMoney(self.Pay)
			if(self.PayDayMessage == nil)then
				Notify(owner, 2, 3, string.format("%s PayDay! You earned $%s", self.PrintName, self.Pay))
			else
				Notify(owner, 2, 3, string.format(self.PayDayMessage, self.Pay))
			end
	else
		Notify(owner, 4, 3, string.format("Your %s is inactive, so no payday for you!", self.PrintName))
	end
end

function ENT:WarnTurnOff()
	if self.World then return end
	self:SetColor(255, 150, 150, 255)
	Notify(self.dt.Owner, 3, 3, string.format("Your %s is about to go inactive!", self.PrintName))
end

function ENT:TurnOff()
	if self.World then return end
	self.Active = false
	self:SetColor(255, 0, 0, 255)
	Notify(self.dt.Owner, 4, 3, string.format("Your %s has gone inactive!", self.PrintName))
end

function ENT:OnTakeDamage(dmginfo)
	local damage = dmginfo:GetDamage()
	local attacker = dmginfo:GetAttacker()
	local inflictor = dmginfo:GetInflictor()
	
	if self.dt.Health > 0 then
		self.dt.Health = self.dt.Health - damage
	else
		self:Explode()
		
		if(attacker == self.dt.Owner) then
			Notify(attacker,4,3,"Paid nothing for destroying your own stuff!")
			return
		elseif (self.Scraps == false && attacker:IsPlayer()) then
			local pay = math.ceil(self.Pay)
			attacker:GiveMoney(pay)
			Notify(attacker,2,3,"Paid $"..tostring(pay).." for destroying a "..self.PrintName)
			return
		elseif (self.Scraps == true && inflictor:GetClass()!="bigbomb" && inflictor:GetClass()!="env_physexplosion") then
			self:MakeScraps()
			return
		end
	end
end

function ENT:Explode()
	local vPoint = self:GetPos()
	local effectdata = EffectData() 
		effectdata:SetStart(vPoint)
		effectdata:SetOrigin(vPoint) 
		effectdata:SetScale(1)
	util.Effect("Explosion", effectdata)
	
	self:Remove()
end

function ENT:MakeScraps()
	local value = self.Cost / 8
	
	if value < 5 then value = 5 end
	
	for i = 1, 6, 1 do
		local scrap = ents.Create("item_scrapmetal")
		scrap:SetModel(string.format("models/gibs/metal_gib%s.mdl", math.random(1, 5)))
		local randpos = Vector(math.random(-5, 5), math.random(-5, 5), math.random(0, 5))	
		scrap:SetPos(self.Entity:GetPos() + randpos)
		scrap:GetTable().Value = math.random(3, value)
		scrap:Spawn()
		scrap:Activate()
		scrap:GetPhysicsObject():SetVelocity(randpos * 35)
	end 
end

function ENT:SetSparking(bool)
	self.dt.Sparking = bool
end